package com.zaofan.utils;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageBrower extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String callback = request.getParameter("CKEditorFuncNum");
		System.out.println(callback);
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<script type='text/javascript'>");
		out.println("function tt(obj){");
		out.println("window.opener.CKEDITOR.tools.callFunction(" + callback + ",obj)");
		out.println("window.close();");
		out.println("}");
		out.println("</script>");
		String uploadDir = getServletContext().getRealPath("/upload");
		File file = new File(uploadDir);
		if (!file.exists()) {
			file.mkdir();
		}
		File[] files = file.listFiles();
		for (File f : files) {
			if (f.isDirectory()) {
				out.print(f.getName());
				File[] filesc = f.listFiles();
				for (File fc : filesc) {
					out.print("<div onclick =tt('" + "upload/" + f.getName() + "/" + fc.getName() + "','')>");
					out.print("upload/" + f.getName() + "/" + fc.getName());
					out.print("</div>");
				}
			}
		}
		out.flush();
		out.close();

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
